package com.home766.service.impl;

import com.home766.dao.SchemeProductDao;
import com.home766.dto.SchemeProductDto;
import com.home766.entity.SchemeProduct;
import com.home766.service.SchemeProductService;
import net.biz.Page;
import net.biz.Pageable;
import net.biz.dao.SpecificationValueDao;
import net.biz.entity.Brand;
import net.biz.entity.SpecificationValue;
import net.biz.service.SnService;
import net.biz.service.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 方案产品-serviceImpl
 */
@Service("schemeProductServiceImpl")
public class SchemeProductServiceImpl extends BaseServiceImpl<SchemeProduct, Long> implements SchemeProductService {
	
	@Resource(name="schemeProductDaoImpl")
	public void setBaseDao(SchemeProductDao schemeProductDao) {
		super.setBaseDao(schemeProductDao);
	}
	
	@Resource(name="schemeProductDaoImpl")
	private SchemeProductDao schemeProductDao;
	@Resource(name="snServiceImpl")
	private SnService snService;
	@Resource(name="specificationValueDaoImpl")
	private SpecificationValueDao specificationValueDao;
	
	/**
	 * 检查名称是否存在
	 * @param name
	 * @return
	 */
	@Override
	public boolean checkExists(String name) {
		return schemeProductDao.checkExists(name);
	}
	
	/**
	 * 根据方案类型查询对应方案产品数据集合
	 * @param type
	 * @return
	 */
	@Override
	public List<SchemeProduct> findSchemeProductByType(Long type, String sn, String name) {
		return schemeProductDao.findSchemeProductByType(type,sn,name);
	}
	
	@Override
	public List<SchemeProduct> findSchemeProductByCategory(Long category, String sn, String name) {
		return schemeProductDao.findSchemeProductByCategory(category,sn,name);
	}

	@Override
	public Page<SchemeProductDto> findPage(Pageable pageable, String sn, String name, String createPerson, Date beginDate, Date endDate, Long type, Long brand) {
		return schemeProductDao.findPage(pageable, sn, name, createPerson, beginDate, endDate, type, brand);
	}
	
	/**
	 * 根据编号查询主材产品
	 */
	@Override
	public SchemeProduct findBySn(String sn) {
		return schemeProductDao.findBySn(sn);
	}
	
	/**
	 * 根据分类查询主材产品集合
	 * @param categoryId
	 * @return
	 */
	@Override
	public List<SchemeProduct> findByCategory(Long categoryId, Boolean bool) {
		return schemeProductDao.findByCategory(categoryId,bool);
	}

	@Override
	public boolean checkBrandInSchemeProduct(Brand brand) {
		return schemeProductDao.checkBrandInSchemeProduct(brand);
	}
	
	/**
	 * 方案产品逻辑删除
	 */
	@Transactional
	@Override
	public void delete(Long... ids) {
		if (ids != null) {
			for (Long id : ids) {
				SchemeProduct sp = schemeProductDao.find(id);
				if(sp != null){
					//设置为删除状态
					sp.setIsDelete(true);
					super.update(sp);
				}
			}
		}
	}

	@Override
	public synchronized boolean saveSchemeProducts(SchemeProduct sproduct,String[] specificationValueIds) {
		if(sproduct != null && specificationValueIds.length > 1){
			for(int i = 1;i < specificationValueIds.length;i++){
				SpecificationValue specVal = specificationValueDao.find(Long.parseLong(specificationValueIds[i]));
				if(specVal != null){
					SchemeProduct sp = new SchemeProduct();
					sp.setCreatePerson(sproduct.getCreatePerson());
					sp.setName(sproduct.getName());
					sp.setNameId(sproduct.getNameId());
					sp.setType(sproduct.getType());
					sp.setIsDelete(false);
					sp.setBrand(sproduct.getBrand());
					sp.setSpecification(sproduct.getSpecification());
					sp.setCategory(sproduct.getCategory());
					sp.setCategoryId(sproduct.getCategoryId());
					sp.setPrice(sproduct.getPrice());
					sp.setImage(sproduct.getImage());
					sp.setIsTehui(sproduct.getIsTehui());
					sp.setIsReward(sproduct.getIsReward());
					sp.setSpecificationValue(specVal);
					sp.setCoefficient(sproduct.getCoefficient());
					sp.setDistType(sproduct.getDistType());
					sp.setSn(snService.generate(net.biz.entity.Sn.Type.product));
					sp.setCreateDate(new Date());
					sp.setModifyDate(new Date());
					super.save(sp);
				}
			}
			return true;
		}
		return false;
	}

	@Override
	public List<SchemeProductDto> getSchemeProductDtoList() {
		// TODO Auto-generated method stub
		return schemeProductDao.getSchemeProductDtoList();
	}

	@Override
	public List<SchemeProductDto> getSchemeProductDtoListOfAuxiliary() {
		// TODO Auto-generated method stub
		return schemeProductDao.getSchemeProductDtoListOfAuxiliary();
	}

	@Override
	public List<SchemeProductDto> getSchemeProductDtoListByConditioner() {
		// TODO Auto-generated method stub
		return schemeProductDao.getSchemeProductDtoListByConditioner();
	}
}
